<template>
	<view>
		<view class="search-box">
			<uni-search-bar radius="100" cancelButton="none" @input="input" clearButton="auto" ></uni-search-bar>
		</view>
		
		<!-- 历史搜索 -->
		<view class="history-box" v-if="SearchList.length === 0">
			<!-- 历史栏 -->
			<view class="history-box-x" v-if="historyList.length !== 0">
				<text class="history-text">搜索历史</text>
				<uni-icons type="trash" size="20" @click="clreaHistory"></uni-icons>
			</view>
			<!-- 无记录 -->
			<view class="history-title" v-else>
				<view> 暂无记录 </view>
			</view>
			<!-- 记录列表 -->
			<view class="history-box-list">
				<uni-tag :text="item" class="history-list-item" v-for="(item,index) in histories" :key="index" @click="bindClick(item)">	</uni-tag>
			</view>
		</view>
		
		<!-- 渲染搜素列表 -->
		<view class="sugg-box" v-else>
			<view class="sugg-item" v-for="(item,i) in SearchList" :key="i" @click="gotoDatlis(item)">
				<!-- 商品的名字 -->
				<view class="good_name"> {{item.goods_name}} </view>
				<!-- 图标 -->
				<uni-icons type="forward" size="24"></uni-icons>
			</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 定义开关
				timer : null,
				// 关键词
				kw:'',
				// 返回的数据列表
				SearchList:[],
				// 历史列表
				historyList:['a','b','c']
			};
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		methods:{
			// 回调search中的值
			input(e){
				clearTimeout(this.timer)
				this.timer=setTimeout(()=>{
					this.kw = e
					// 请求
					this.getSearchList()
					this.sreachHistory()
				},500)
			},
			async getSearchList(){
				// 判断搜素的关键字
				if(this.kw.length === 0){
					this.SearchList = []
					return
				}
				let {data:res} = await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.kw})
				if(res.meta.status !== 200) return uni.$showMsg()
				this.SearchList = res.message
			},
			// 跳转商品详情
			gotoDatlis(item){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
				})
			},
			// 历史记录发起请求
			bindClick(item){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query='+item
				})
			},
			// 改变历史记录
			sreachHistory(){
				let set = new Set([...this.historyList]);
				set.delete(this.kw)
				set.add(this.kw)
				this.historyList = Array.from(set)
				// 将数据持久化
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			// 清除历史记录
			clreaHistory(){
				this.historyList = []
				uni.removeStorageSync('kw')
			}
		},
		computed:{
			histories(){
				return [...this.historyList].reverse()
			}
		}
		
	}
</script>

<style lang="scss">
	// 搜索栏固定位置
.search-box{
	position: sticky;
	top: 0;
	z-index: 999;
}
// 搜索列表
.sugg-box{
	padding: 0 2px;
	.sugg-item{
		font-size: 12px;
		display: flex;
		padding: 10px 0;
		border-bottom: 1px solid #bbb;
		justify-content: space-between;
		align-items: center;
		.good_name{
			overflow: hidden;
			white-space: nowrap;
			text-overflow: ellipsis;
			margin: 3px;
		}
	}
}
// 历史记录
.history-box{
	padding: 0 5px;
	.history-box-x{
		display: flex;
		justify-content: space-between;
		height: 30px;
		align-items: center;
		font-size: 13px;
		border-bottom: 1px solid #ccc;
	}
	.history-title{
		position: absolute;
		top: 48%;
		width: 100%;
		display: flex;
		justify-content: center;
	}
	.history-box-list{
		display: flex;
		// 允许换行
		flex-wrap: wrap;
		.history-list-item{
			border-radius: 5px;
			margin-right: 5px;
			margin-top: 5px;		
		}
		
	}
}
</style>
